package com.chenjun.designpattern.behavior.template;

/**
 * Demo2
 * 
 * @author Administrator
 *
 */

public class 模板模式2
{
	public static void main(String[] args)
	{
		Benchmark benchmark = new MethodBenchmark();
		benchmark.repeat(10);
	}
}

/**
 * 定义一个基准类
 * 
 * @author Administrator
 *
 */
abstract class Benchmark
{
	/**
	 * 有一个待实现的基准抽象方法
	 */
	public abstract void benchmark();

	/**
	 * 设置该类为final 终态，无法被继承
	 * 
	 * @param count
	 * @return
	 */
	public final Long repeat(int count)
	{
		if (count <= 0)
		{
			return 0L;
		}
		else
		{
			Long startTime = System.currentTimeMillis();
			for (int i = 0; i < count; i++)
			{
				this.benchmark();
			}
			Long endTime = System.currentTimeMillis();
			return endTime - startTime;
		}
	}
}

class MethodBenchmark extends Benchmark
{
	@Override
	public void benchmark()
	{
		for (int i = 0; i < Short.MAX_VALUE; i++)
		{
			System.out.println("i = " + i);
		}
	}
}
